package vn.student.controller;

import java.util.Date;
import vn.student.AStudentDB;
import vn.student.R;
import vn.student.mylib.Semester;
import android.app.Activity;
import android.widget.TextView;

public class ViewEditSemesterDetailEditorController {
	Activity act = null;
	String id;
	AStudentDB myDB = null;
	public Semester semesterInfo = null;
	boolean isCreateNew = false;
	public ViewEditSemesterDetailEditorController(Activity act,String id,boolean isCreateNew){
		this.act = act;
		this.id = id;
		this.isCreateNew = isCreateNew;
		myDB = new AStudentDB(act);
		if(!isCreateNew)
			semesterInfo = myDB.getSemesterDetail(id);
		else{
			Date tmp = new Date();
			String dateStr = "";
			String str = "";
			str = String.valueOf(tmp.getYear()+1900);
			dateStr += str + "-";
			str = String.valueOf(tmp.getMonth()+1);
			str = str.length()==2?str:("0"+str);
			dateStr += str + "-";
			str = String.valueOf(tmp.getDate());
			str = str.length()==2?str:("0"+str);
			dateStr += str;
			semesterInfo = new Semester("","New semester",dateStr,dateStr);
		}
	}
	public void showCourseDetail(String id) {			
		if(semesterInfo==null)
			return;
		TextView tmpLabel = (TextView) act.findViewById(R.id.nameLabel);
		tmpLabel.setText(semesterInfo.semesterName);
		tmpLabel = (TextView) act.findViewById(R.id.durationLabel);
		tmpLabel.setText(semesterInfo.dateStart +" to " + semesterInfo.dateEnd);
	}
	public void editCourseName(String name) {
		semesterInfo.semesterName = name;
	}
	public void editDuration(String dateStartStr, String dateEndStr) {
		// TODO Auto-generated method stub
		semesterInfo.dateStart = dateStartStr;
		semesterInfo.dateEnd = dateEndStr;
	}
	public void showSemesterDetail() {
		if(semesterInfo==null)
			return;
		TextView tmpLabel = (TextView) act.findViewById(R.id.nameLabel);
		tmpLabel.setText(semesterInfo.semesterName);
		tmpLabel = (TextView) act.findViewById(R.id.durationLabel);
		tmpLabel.setText(semesterInfo.dateStart +" to " + semesterInfo.dateEnd);
	}
	public boolean updateSemesterInfo() {
		if(isCreateNew){
			return myDB.addNewSemester(semesterInfo);
		}
		return myDB.updateSemesterDetail(semesterInfo);
	}
}
